﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApp1.Theme
{

    public class BaseForm : Form
    {
        // 边缘检测宽度（像素）
        private const int BorderWidth = 8;

        // 窗体缩放状态
        private bool _isResizing = false;
        private ResizeDirection _resizeDir;

        // 最小窗体尺寸
        private readonly Size _minSize = new Size(200, 150);

        // 缩放方向枚举（精简版）
        private enum ResizeDirection
        {
            None,
            Left,
            Right,
            Top,
            Bottom,
            BottomRight
        }

        public BaseForm()
        {
            FormBorderStyle = FormBorderStyle.None;
            DoubleBuffered = true;
        }

        // 检测鼠标位置（仅保留需要的方向）
        private ResizeDirection GetResizeDirection(Point mousePos)
        {
            bool isLeft = mousePos.X <= BorderWidth;
            bool isRight = mousePos.X >= ClientSize.Width - BorderWidth;
            bool isTop = mousePos.Y <= BorderWidth;
            bool isBottom = mousePos.Y >= ClientSize.Height - BorderWidth;

            // 仅检测左右上下和右下角
            if (isRight && isBottom) return ResizeDirection.BottomRight;
            if (isLeft) return ResizeDirection.Left;
            if (isRight) return ResizeDirection.Right;
            if (isTop) return ResizeDirection.Top;
            if (isBottom) return ResizeDirection.Bottom;

            return ResizeDirection.None;
        }

        // 根据方向设置光标
        private void UpdateCursor(ResizeDirection dir)
        {
            switch (dir)
            {
                case ResizeDirection.Left:
                case ResizeDirection.Right:
                    Cursor = Cursors.SizeWE;
                    break;
                case ResizeDirection.Top:
                case ResizeDirection.Bottom:
                    Cursor = Cursors.SizeNS;
                    break;
                case ResizeDirection.BottomRight:
                    Cursor = Cursors.SizeNWSE;
                    break;
                default:
                    Cursor = Cursors.Default;
                    break;
            }
        }

        // ===== 事件重写 ===== //
        protected override void OnMouseMove(MouseEventArgs e)
        {
            // 更新光标状态（非缩放状态下）
            if (!_isResizing)
            {
                var dir = GetResizeDirection(e.Location);
                UpdateCursor(dir);
            }

            // 处理缩放
            if (_isResizing)
            {
                int newWidth = Width;
                int newHeight = Height;
                int newX = Left;
                int newY = Top;

                switch (_resizeDir)
                {
                    case ResizeDirection.Left:
                        // 左侧：调整宽度和X坐标
                        newWidth -= e.X;
                        newX += e.X;
                        break;
                    case ResizeDirection.Right:
                        // 右侧：只调整宽度
                        newWidth = e.X + (Width - ClientSize.Width);
                        break;
                    case ResizeDirection.Top:
                        // 顶部：调整高度和Y坐标
                        newHeight -= e.Y;
                        newY += e.Y;
                        break;
                    case ResizeDirection.Bottom:
                        // 底部：只调整高度
                        newHeight = e.Y + (Height - ClientSize.Height);
                        break;
                    case ResizeDirection.BottomRight:
                        // 右下角：同时调整宽度和高度
                        newWidth = e.X + (Width - ClientSize.Width);
                        newHeight = e.Y + (Height - ClientSize.Height);
                        break;
                }

                // 应用新尺寸（确保不小于最小值）
                newWidth = Math.Max(newWidth, _minSize.Width);
                newHeight = Math.Max(newHeight, _minSize.Height);

                if (_resizeDir == ResizeDirection.Left || _resizeDir == ResizeDirection.Top)
                {
                    // 需要修改坐标的边
                    SetBounds(newX, newY, newWidth, newHeight);
                }
                else
                {
                    // 仅改变大小的边
                    Size = new Size(newWidth, newHeight);
                }
            }
            base.OnMouseMove(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _resizeDir = GetResizeDirection(e.Location);
                _isResizing = (_resizeDir != ResizeDirection.None);
            }
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            _isResizing = false;
            Cursor = Cursors.Default;
            base.OnMouseUp(e);
        }

        // 双击右下角区域关闭窗体（可选功能）
        protected override void OnDoubleClick(EventArgs e)
        {
            if (GetResizeDirection(PointToClient(MousePosition)) == ResizeDirection.BottomRight)
            {
                Close();
            }
            base.OnDoubleClick(e);
        }
    }
}